/**
 *  @author Cornelius Huang
 *  attachment upload module
 */
app.directive('attachUpload', function() {
	return {
		restrict: 'AE',
		transclude: false,
		templateUrl: './views/fileUpload.html',
		scope: {
			fileOut: '&?'
		},
		controller: ['$scope', function($scope) {
			//scope.fileName = fileInfo.name;				
					
		}],
		link: function(scope, element, attrs) {
			var file = document.querySelector('.attachment');
			var attachBtn = document.querySelector('.mal-attach');
			var removeBtn = document.querySelector('.rmv-btn');
			
			attachBtn.addEventListener('click', function(event) {
				console.log('attachBtn');
				file.click();
			});
			
			file.addEventListener('change', function(event) {
				console.log(event);
				console.log(this.files[0]);
				var fileInfo = this.files;
				
				scope.fileOut({file: fileInfo});				
				scope.fileName = fileInfo[0].name;
				scope.$apply();
				
				if(file.files.length !== 0) {
					removeBtn.classList.add('rmv-btn-show');
				}
			});
			
			removeBtn.addEventListener('click', function() {
				file.value = '';
				console.log(file.files);
				
				if(file.files.length === 0) {
					scope.fileName = null;
					scope.fileOut({file: file.files});
					scope.$apply();
				}
				
				this.classList.remove('rmv-btn-show');
			})
		}
		
	}
});